package com.atguigu.service.impl.product;

import com.atguigu.dto.product.ProductDto;
import com.atguigu.entity.product.Product;
import com.atguigu.entity.product.ProductDetails;
import com.atguigu.entity.product.ProductSku;
import com.atguigu.mapper.product.ProductDetailsMapper;
import com.atguigu.mapper.product.ProductSkuMapper;
import com.atguigu.mapper.product.ProductMapper;
import com.atguigu.service.product.ProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductSkuMapper productSkuMapper;

    @Autowired
    private ProductDetailsMapper productDetailsMapper;

    @Override
    public void submit(Product product) {
        product.setStatus(0);
        product.setAuditStatus(0);
        productMapper.submit(product);

        List<ProductSku> productSkuList = new ArrayList<>();
        for (int i = 0; i < product.getProductSkuList().size(); i++) {
            ProductSku productSku = productSkuList.get(i);
            productSku.setSkuCode(product.getId() + "" + i);
            productSku.setProductId(product.getId());
            productSku.setSkuName(product.getName() + productSku.getSkuName());
            productSku.setSaleNum(0);
            productSku.setStatus(0);
            productSkuMapper.submit(productSku);
        }
        ProductDetails productDetails = new ProductDetails();
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetails.setProductId(product.getId());
        productDetailsMapper.submit(productDetails);
    }

    @Override
    public void remove(Long productId) {
        productMapper.remove(productId);
        productSkuMapper.removeByProduct(productId);
        productDetailsMapper.removeByProduct(productId);
    }

    @Override
    public void update(Product product) {
        productMapper.updateById(product);

        List<ProductSku> productSkuList = product.getProductSkuList();
        productSkuList.forEach(productSku -> productSkuMapper.updateByProductId(productSku));

        ProductDetails productDetails = productDetailsMapper.findDetailByProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailsMapper.updateByProductId(productDetails);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        Product product = new Product();
        product.setId(id);
        if (status == 1) {
            product.setStatus(1);
            product.setAuditMessage("上架成功");
        } else {
            product.setStatus(-1);
            product.setAuditMessage("下架成功");
        }
        productMapper.updateById(product);
    }

    @Override
    public void updateAuditStatus(Long id, Integer auditStatus) {
        Product product = new Product();
        product.setId(id);
        if (auditStatus == 1) {
            product.setStatus(1);
            product.setAuditMessage("审批通过");
        } else {
            product.setStatus(-1);
            product.setAuditMessage("审批不通过");
        }
        productMapper.updateById(product);
    }

    @Override
    public Product detail(Long productId) {
        Product product = productMapper.findDetailById(productId);

        List<ProductSku> productSkuList = productSkuMapper.findDetailByProductId(productId);
        product.setProductSkuList(productSkuList);

        ProductDetails productDetails = productDetailsMapper.findDetailByProductId(productId);
        product.setDetailsImageUrls(productDetails.getImageUrls());

        return product;
    }

    @Override
    public PageInfo<Product> getPage(Integer current, Integer limit, ProductDto productDto) {
        PageHelper.startPage(current, limit);
        List<Product> productList = productMapper.findAll(productDto);
        return new PageInfo<Product>(productList);
    }
}
